package com.scs.application.modules.sys.controller;

import com.scs.application.core.annotation.PassToken;
import com.scs.application.core.utils.FileUtils;
import com.scs.application.modules.sys.dto.LoginParam;
import com.scs.application.modules.sys.dto.SingleLoginParam;
import com.scs.application.modules.sys.model.RestResult;
import com.scs.application.modules.sys.service.UserService;
import com.scs.application.modules.sys.vo.LoginResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 */
@RestController
public class LoginController {
    @Value("${res.real-path}")
    private String resPath;
    @Autowired
    private UserService userService;

    @PassToken
    @GetMapping(value = "/login")
    public RestResult login(@Valid LoginParam loginParam) {
        LoginResult loginResult = userService.login(loginParam.getCode(), loginParam.getPassword(),loginParam.getIsCheck(),loginParam.getDeviceType());        return RestResult.ok("登录成功",loginResult);
    }

    @GetMapping(value = "/singleLogin")
    public RestResult loginForSingleSignOn(@Valid SingleLoginParam loginParam) {
        LoginResult loginResult = userService.SingleSignOn(loginParam.getTicket());        return RestResult.ok("登录成功",loginResult);
    }


    @PassToken
    @GetMapping(value = "/singleLoginOut")
    public void loginOutForSingleSignOn(String deviceType) {
        userService.logout();
    }
    /**
    * @Description：获取登录界面的一些资源, resPath/01login
    */
    @PassToken
    @GetMapping(value = "/getLoginIndexResource")
    public RestResult getLoginIndexResource() {
        return RestResult.ok("操作成功", FileUtils.getFileNames(resPath+"01login","01login/"));
    }
}
